<!DOCTYPE html>
<html>
<head>
    <title>Physics Diagram</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        canvas {
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <canvas id="physicsCanvas" width="500" height="420"></canvas>
    <script>
        const canvas = document.getElementById('physicsCanvas');
        const ctx = canvas.getContext('2d');

        // Function to draw an arrow
        function drawArrow(ctx, fromx, fromy, tox, toy, arrowWidth = 5, color = 'black') {
            const headlen = 10;
            const dx = tox - fromx;
            const dy = toy - fromy;
            const angle = Math.atan2(dy, dx);

            ctx.save();
            ctx.strokeStyle = color;
            ctx.fillStyle = color;

            // Line
            ctx.beginPath();
            ctx.moveTo(fromx, fromy);
            ctx.lineTo(tox, toy);
            ctx.lineWidth = 2;
            ctx.stroke();

            // Arrowhead
            ctx.beginPath();
            ctx.moveTo(tox, toy);
            ctx.lineTo(tox - headlen * Math.cos(angle - Math.PI / 6), toy - headlen * Math.sin(angle - Math.PI / 6));
            ctx.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6), toy - headlen * Math.sin(angle + Math.PI / 6));
            ctx.closePath();
            ctx.fill();
            ctx.restore();
        }

        // --- Main Drawing ---

        // Clear canvas
        ctx.fillStyle = 'white';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        
        // Define drawing properties
        ctx.strokeStyle = 'black';
        ctx.fillStyle = 'black';
        ctx.lineWidth = 2;
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';

        // Ground
        const groundY = 350;
        ctx.beginPath();
        ctx.moveTo(0, groundY);
        ctx.lineTo(canvas.width, groundY);
        ctx.stroke();

        // Hatching for the ground
        ctx.lineWidth = 1;
        for (let i = 0; i < canvas.width; i += 15) {
            ctx.beginPath();
            ctx.moveTo(i, groundY);
            ctx.lineTo(i + 10, groundY + 10);
            ctx.stroke();
        }
        ctx.lineWidth = 2; // Reset line width

        // Block C
        const c = { w: 240, h: 160, x: 90 };
        c.y = groundY - c.h;
        ctx.strokeRect(c.x, c.y, c.w, c.h);
        ctx.font = '28px serif';
        ctx.fillText('C', c.x + c.w / 2, c.y + c.h / 2);

        // Block A
        const a = { w: 90, h: 50 };
        a.x = c.x + 40;
        a.y = c.y - a.h;
        ctx.strokeRect(a.x, a.y, a.w, a.h);
        ctx.fillText('A', a.x + a.w / 2, a.y + a.h / 2);

        // Pulley
        const pulleyRadius = 15;
        const pulleyCenter = { x: c.x + c.w, y: c.y };
        ctx.beginPath();
        ctx.arc(pulleyCenter.x, pulleyCenter.y, pulleyRadius, -Math.PI, Math.PI / 2);
        ctx.stroke();

        // Block B
        const b = { w: 60, h: 70 };
        b.x = pulleyCenter.x + pulleyRadius + 30;
        b.y = c.y + 40;
        ctx.strokeRect(b.x, b.y, b.w, b.h);
        ctx.fillText('B', b.x + b.w / 2, b.y + b.h / 2);

        // String connecting A and B
        ctx.beginPath();
        // Horizontal part from A to pulley
        const stringY = a.y + a.h / 2;
        ctx.moveTo(a.x + a.w, stringY);
        ctx.lineTo(pulleyCenter.x, stringY);
        // Vertical part from pulley to B
        const stringX = pulleyCenter.x + pulleyRadius;
        ctx.moveTo(stringX, pulleyCenter.y);
        ctx.lineTo(stringX, b.y);
        ctx.stroke();


        // Force F
        const forceY = c.y + c.h / 2;
        drawArrow(ctx, 20, forceY, c.x, forceY);
        ctx.font = 'italic 28px serif';
        ctx.fillText('F', 35, forceY - 20);

        // Bottom Label
        ctx.font = '24px "KaiTi", "SimSun", sans-serif';
        ctx.fillStyle = 'black';
        ctx.fillText('习题 1-31 图', canvas.width / 2, groundY + 40);

    </script>
</body>
</html>